<?php
class Tanks extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tanks';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('tank_name, site_ID, capacity, alert_capacity, warning_capacity, tank_position_ID, tank_type_ID, pump_provided, tank_drop_method_ID, product_ID, inventory_last_read, inventory_last_time, remote_fill, overfill_alarm', 'required', 'on'=>'create'),
			array('tank_name, capacity, tank_position_ID, tank_type_ID, pump_provided, product_ID, remote_fill, overfill_alarm', 'required', 'on'=>'update'),
			array('site_ID, tank_monitor_ID, tank_position_ID, tank_type_ID, tank_drop_method_ID, product_ID, tank_monitor_ID', 'numerical', 'min'=>0, 'integerOnly'=>true, 'on'=>'create, update'),
			array('capacity, alert_capacity, warning_capacity', 'numerical', 'numberPattern'=>'/^\d{1,9}+(\.\d{1,6})?$/', 'on'=>'create, update', 'message'=>'the capacity field at most has 9 digits before the decimal point and 6 digits after the decimal point'),
			array('pump_provided, remote_fill, overfill_alarm','boolean', 'trueValue'=>1, 'falseValue'=>0 ,'on'=>'create, update'),
		);
	}
	
	public function relations()
	{
		return array(
			'tankMonitor'=>array(self::BELONGS_TO, 'TankMonitors', 'tank_monitor_ID'),
			'tankPosition'=>array(self::BELONGS_TO, 'TankPositions', 'tank_position_ID'),
			'tankType'=>array(self::BELONGS_TO, 'TankTypes', 'tank_type_ID'),
			'tankDropMethod'=>array(self::BELONGS_TO, 'TankDropMethods', 'tank_drop_method_ID'),
			'product'=>array(self::BELONGS_TO, 'Products', 'product_ID'),
			'site'=>array(self::BELONGS_TO, 'Sites', 'site_ID'),
			'auctions'=>array(self::HAS_MANY, 'Auctions', 'tank_ID'),
		);
	}
	
	public function loadTankNames()
	{
		$connection=Yii::app()->db;
		$siteID = Sites::model()->getSiteID(Yii::app()->user->userID);
		$sql = "SELECT tank_ID, tank_name FROM ".Tanks::model()->tableName()." WHERE site_ID='".$siteID."'";
		$command = $connection->createCommand($sql);
		$dataReader = $command->query();
		$res = array('-1'=>'select a tank\'s name');
		foreach($dataReader as $row)
		{
			$key=$row['tank_ID'];
			$res[$key]=$row['tank_name'];
		}
		return $res;
	}
	
	public function getTankInfo($tankID)
	{
		$record = $this->with('product', 'site')->findByPk($tankID);
		$res = array();
		if($record!=null)
		{
			$res[]=$record->product->product_ID;
			$res[]=$record->product->product_name;
			$res[]=$record->site->site_ID;
			$res[]=$record->site->site_name;
			$res[]=$record->capacity;
			$res[]=$record->inventory_last_read;
			$res[]=$record->inventory_last_time;
		}
		return $res;
	}
	
	public function getTankName($tankID) {
		$sql = "SELECT tank_name FROM ".$this->tableName()." WHERE tank_ID='".$tankID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $row) {
			$tankName = $row->tank_name;
		}
		
		return $tankName;
	}
}
?>
